﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using Microsoft.Win32;


namespace WpfPdfReader
{
    [RunInstaller(true)]
    public partial class WpfPdfReaderInstaller : System.Configuration.Install.Installer
    {
        private const string AppName = "WpfPdfReader.exe";

        private const string menuName = "Wpf PdfReader";

        private string AppDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\WpfPdfReader";

        public WpfPdfReaderInstaller()
        {
            InitializeComponent();
        }

        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            string dir = Context.Parameters["targetdir"];

            if (!string.IsNullOrEmpty(dir))
            {
                string apppath = "";
                apppath = System.IO.Path.Combine(dir.Trim(), AppName);
                //注册系统右键菜单
                RegMenuRight(menuName, apppath);
            }
            else
                throw new Exception("no dir");

            //删除原系统数据，以免在新的版本中发生冲突
            if (System.IO.Directory.Exists(this.AppDataFolder))
            {
                try
                {
                    System.IO.Directory.Delete(this.AppDataFolder, true);
                }
                catch { }
            }
        }
        /// <summary>
        /// 注册启动程序到系统菜单右键方法
        /// </summary>
        /// <param name="menuName">菜单名</param>
        private void RegMenuRight(string menuName, string appPath)
        {
            //注册到文件
            RegistryKey shellKey = Registry.ClassesRoot.CreateSubKey(@"*\shell");
            RegistryKey menuKey = shellKey.CreateSubKey(menuName);
            RegistryKey cmd = menuKey.CreateSubKey("command");
            cmd.SetValue("", appPath + " \"%1\"");//appPath 是本程序自身的路径  %1是传入打开的文件路径
            cmd.Close();
            menuKey.Close();
            shellKey.Close();
        }

        private void UnRegMenuRight(string menuName)
        {
            RegistryKey shellKey = Registry.ClassesRoot.CreateSubKey(@"*\shell");
            RegistryKey menuKey = shellKey.OpenSubKey(menuName);
            if (menuKey != null)
            {
                menuKey.Close();
                shellKey.DeleteSubKeyTree(menuName);
            }
            shellKey.Close();
        }

        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
        }
        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
            UnRegMenuRight(menuName);
            if (System.IO.Directory.Exists(this.AppDataFolder))
            {
                try
                {
                    System.IO.Directory.Delete(this.AppDataFolder, true);
                }
                catch { }
            }
        }
    }
}
